{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Matrix Multiplication, with Jax\n",
    "\n",
    "This runs a large number of batched matrix multiplcations with Jax.  Jax does have support for sparse matrices, but for the matrix sizes we're interested in they aren't a good performance tradeoff.\n",
    "\n",
    "This can run the benchmark both on CPU and GPU.\n",
    "\n",
    "See [the paper](https://symforce.org/paper) for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as onp\n",
    "from jax import numpy as np\n",
    "import jax\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set to CPU\n",
    "# Comment out to use GPU/TPU\n",
    "jax.config.update(\"jax_platform_name\", \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Print the platform (CPU/GPU) we're using\n",
    "jax.lib.xla_bridge.get_backend().platform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def time_func(f, key, calls):\n",
    "    start = time.perf_counter()\n",
    "    for _ in range(calls):\n",
    "        f(key)\n",
    "        _, key = jax.random.split(key)\n",
    "    end = time.perf_counter()\n",
    "    return (end - start) / calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = jax.random.PRNGKey(42)\n",
    "\n",
    "for N in reversed([1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7]):\n",
    "    N = int(N)\n",
    "    mat_size_m = 20\n",
    "    mat_size_n = 15\n",
    "\n",
    "    A = jax.random.normal(key, (N, mat_size_m, mat_size_n))\n",
    "    _, key = jax.random.split(key)\n",
    "    B = jax.random.normal(key, (N, mat_size_n, mat_size_m))\n",
    "    _, key = jax.random.split(key)\n",
    "\n",
    "    np.matmul(A, B)\n",
    "\n",
    "    def matmul(key):\n",
    "        A_new = A.at[0, 0, 0].set(jax.random.normal(key))\n",
    "        return np.matmul(A_new, B)\n",
    "\n",
    "    t = time_func(matmul, key, 10)\n",
    "\n",
    "    def notmul(key):\n",
    "        A_new = A.at[0, 0, 0].set(jax.random.normal(key))\n",
    "        return A_new\n",
    "\n",
    "    _, key = jax.random.split(key)\n",
    "    t2 = time_func(notmul, key, 10)\n",
    "\n",
    "    print(f\"{N:>10}   {t:10.5} {t2:10.5} {t - t2:10.5} {(t - t2) / N:10.5}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "symforce-paper",
   "language": "python",
   "name": "symforce-paper"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
